Skip to content

feat(mcp): forward hooks_timeout on laya_route_batch - #770

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-mcp-route-batch-hooks
Oct 1, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-mcp-route-batch-hooks

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

laya_route_batch now accepts and forwards hooks_timeout, matching what its batch sibling laya_predict_batch does and what Router.route_batch has always taken.

Why

Router.route_batch grows a hooks_timeout parameter (laya/router.py:911, docstring :925-926) that overrides the Router's own deadline for every request's on_route dispatch. The single-request laya_route has a route-only path; the batch tool never exposed it, so a caller sweeping routing decisions over a workload cannot protect against a slow or hung operator hook in the same way they can on the predict path.

Concretely: an operator has an on_route hook that does an outbound check. On a bad network, the Router's configured deadline is, say, 2 s; a caller running a 500-request laya_route_batch to inspect a workload's routing before paying load cost gets stuck at 2 s × N with no way to say "give me the aggregate faster, use 200 ms." laya_predict_batch can already do this; laya_route_batch could not.

How it was verified

uv run python3 tests/test_mcp.py
# 781 passed, 0 failed
uv run python3 tests/test_hooks_api.py
# all hook API tests passed
uv run python3 tests/test_cli.py
# all CLI tests passed
uv run python3 tests/test_router.py
# all routing tests passed
uv run ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
# All checks passed!
uv run python3 -m compileall -q laya/ tests/
# (silent, exit 0)

Four mutations were run against laya/mcp/tools.py; each produced a named FAIL on the restored tree, not a crash:

Mutation Named check that dies
Revert to bare route_batch(items) (never forwards) batch/route_hooks_timeout_forwarded, batch/route_hooks_timeout_int_coerced
Always forward hooks_timeout even when None batch/route_strict_router_no_kwarg
bool accepted by falling through to core's validate_timeout batch/route_bad_hooks_timeout_True (plus 4 code-name checks)
Validator removed entirely All six batch/route_bad_hooks_timeout_* — ValueError / TypeError surface raw, not as invalid_hooks_timeout

Design notes

  • _validate_hooks_timeout wraps core's laya.hooks.validate_timeout and turns its ValueError into a ToolError("invalid_hooks_timeout", ...), so an MCP caller gets a real code back instead of internal_error: ValueError.
  • Booleans are rejected at the tool boundary. Core's validate_timeout(True) silently coerces to 1.0 and gives a caller a one-second deadline they did not ask for — the same pattern _validate_batch_size guards.
  • hooks_timeout is forwarded only when the caller set one, so a pre-feat(agent): per-call prediction hooks on predict_long, and no window for a skipped answer #577 attached Router (or a stub) still accepts the call. The StrictRouteRouter fixture is the witness for this; without it, the always-forward mutation would pass the suite.
  • The tool schema description mentions the parameter and says what it protects against ("a slow or hung operator hook cannot stall a routing pass the caller only wants the checkpoint labels for"), so clients discover the knob.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main
  • Tests pass locally
  • Docs or examples updated when the public API changed (tool schema description updated in laya/mcp/server.py)

Fixes #

Router.route_batch takes hooks_timeout (router.py:911, docstring 925-926)
and applies it per-request during on_route dispatch. The batch sibling
laya_predict_batch has always had the override plumbed; laya_route_batch
did not. The tool layer accepted only `requests`, so an operator running a
routing sweep over a workload cannot protect against a slow or hung
operator hook the way they can on the predict path.

- New optional `hooks_timeout` arg on laya_route_batch; forwarded to
  Router.route_batch only when the caller actually asked for one, so a
  pre-NandhaKishorM#577 attached Router keeps working.
- _validate_hooks_timeout wraps core's validate_timeout and turns its
  ValueError into a ToolError code (`invalid_hooks_timeout`) at the MCP
  boundary. Bools rejected here (not by core, which coerces True -> 1.0).
- Server wrapper exposes hooks_timeout; the tool description now mentions
  it so clients discover the knob.
- Tests: BatchRouter.route_batch records the timeout; six new checks cover
  forwarding (float + coerced int), default-stays-unset, validation errors,
  and a strict-signature Router stub as the witness against always-kwarg.
  Four mutations each produce a named FAIL.
@NandhaKishorM
NandhaKishorM merged commit c69a993 into NandhaKishorM:main Oct 1, 2026
24 of 25 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged for 0.3.23.

Verified on the integrated branch, not on the PR in isolation: every suite in the ci.yml test lane plus the pytest lane green, compileall clean, and a decision diff against main on the real english and multilingual checkpoints (72 answers over choice, score and noul, short and long states, budgeted and predict_long) showing zero decision flips and a max probability delta of 0.000000.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants